# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Jürgen Pfeifer <juergen@familiepfeifer.de>
# Contributor: Simon Sobisch <simonsobisch@gnu.org>

_realname=gnucobol
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=3.1rc1
pkgver_real=3.1-rc1
pkgrel=1
pkgdesc="GnuCOBOL, a free and modern COBOL compiler (mingw-w64)"
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-svn" "${MINGW_PACKAGE_PREFIX}-gnu-cobol-svn")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-svn" "${MINGW_PACKAGE_PREFIX}-gnu-cobol-svn")
arch=('any')
url="https://www.gnu.org/software/gnucobol/"
options=('strip')
license=('GPL3' 'LGPL3')
# note: the final binaries will call GCC, therefore it is a dependency
depends=("${MINGW_PACKAGE_PREFIX}-gcc"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-gettext"
         "${MINGW_PACKAGE_PREFIX}-ncurses"
         "${MINGW_PACKAGE_PREFIX}-db")
# the optional depencies cannot be used for creating the binary packages, moved to depends
#optdepends=("${MINGW_PACKAGE_PREFIX}-gettext: support for internationalization"
#            "${MINGW_PACKAGE_PREFIX}-ncurses: support for extended screenio"
#            "${MINGW_PACKAGE_PREFIX}-pdcurses: support for screenio")
#source=("https://ftp.gnu.org/gnu/${_realname}/${_realname}-${pkgver}.tar.xz"{,.sig}
source=("https://alpha.gnu.org/gnu/${_realname}/${_realname}-${pkgver_real}.tar.xz"{,.sig}
        "001-mingw-gnucobol-2.2-fixformatwarnings.patch"
        "cobenv.sh" "cobenv.cmd")
sha256sums=('C2E41C2BA520681A67C570D7246D25C31F7F55C8A145AAEC3F6273A500A93A76'
            SKIP
            '3FBFDAF61EB6EC125258DE62B404C97BF6DCCF39BD3B9152510F794B5383CA33'
            'EEDFC170CFD6606527DD701C3CF6BBA2029C33CE694F697F8B7EE527B4ED7F1C'
            '225F7F6E17FC125AF6348028FDB86278C990BEAB07D230A158D9B9373CD58506')
validpgpkeys=('B9459D0CA8A740B323235CDF13E96B53C005604E')

prepare() {
  cd "${_realname}-${pkgver_real}"
  # disabled patch for now, doesn't seem useful any moe
  # patch -Np1 -i ${srcdir}/001-mingw-gnucobol-2.2-fixformatwarnings.patch
}

build() {
  export lt_cv_deplibs_check_method='pass_all'   # libncurses with unconforming name
  [[ -d ${srcdir}/build-${CARCH} ]] && rm -rf ${srcdir}/build-${CARCH}
  cp -prf ${_realname}-${pkgver_real} build-${CARCH}
  cd ${srcdir}/build-${CARCH}
  ./configure \
    CPPLAGS="-D__USE_MINGW_ANSI_STDIO=1" \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --infodir=${MINGW_PREFIX}/share/info \
    --mandir=${MINGW_PREFIX}/share/man \
    --disable-rpath \
    --enable-shared \
    --disable-static
    #  --> as using the static libcob would be the builtin default,
    #      all generated COBOL modules would be GPLed (and big)...
    #      ... and only work when all linked together

  #make -j1
  make "--jobs=$(($(nproc)+1))"
}

check() {
  cd ${srcdir}/build-${CARCH}
  # note: strangely multiple tests seem to halt the complete package creation
  #make check TESTSUITEFLAGS="--jobs=$(($(nproc)+1))"
  make check
}

package() {
  cd ${srcdir}/build-${CARCH}
  make DESTDIR="${pkgdir}" install

  cd ${srcdir}
  install -p -m777 cobenv.* "${pkgdir}${MINGW_PREFIX}/bin"

  cd ${_realname}-${pkgver_real}
  install -d "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
  install -p -m644 COPYING* "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"

  install -d "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}"
  install -p -m644 README NEWS "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}"
  cd doc
  install -p -m644 gnucobol*.pdf "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}"
}
